/*

 Program figs.do makes figures.

 Kumler, Verhoogen, Frias "Enlisting Employees ..." REStat forthcoming

*/


************** housekeeping *************;

#delimit;
set more off;

do ${code}housekeeping.do;

set mem 8g;
set processors 8;

quietly do programs.do;

tempfile d1 d2 d3 d4 d5 d6 d7;


****************************************************************************;
****************************************************************************;
***************** Figures in main text *************************************;
****************************************************************************;
****************************************************************************;

*****************************************************************************************************;
********* Fig 1 (fpensions_male`male'): worker benefit schedules and distribution of pensions *******;
*****************************************************************************************************;

************ benefit schedules as function of final real avg wage **********;

*** get benefit files ready;

clear all;
use ${work}imss_benefits;
sort year qtr;
tab qtr;
save `d1', replace;

clear all;
use ${raw}imss_benefits_sched_ev_2011_09_26.dta;
sort max_mw;
save `d2', replace;

*** get minimum wages file ready;

* just keeping minimum wages for zone A (Mexico City);

clear all;
use ${work}minwages_wtopcodes;
keep if zona=="A";
keep year qtr salmin rsalmin cpi;
sort year qtr;
tab qtr;
save `d3', replace;

*save local for 1997 minimum wage to use in calculating post-97 benefits;

qui sum salmin if year==1997;
local salmin97 = r(mean);
qui sum cpi if year==1997;
local cpi97 = r(mean);

qui sum rsalmin if year==1989 & qtr==2;
local rsalmin1989 = r(mean);
qui sum rsalmin if year==1990 & qtr==2;
local rsalmin1990 = r(mean);
qui sum rsalmin if year==1991 & qtr==2;
local rsalmin1991 = r(mean);
qui sum rsalmin if year==1992 & qtr==2;
local rsalmin1992 = r(mean);
qui sum rsalmin if year==1993 & qtr==2;
local rsalmin1993 = r(mean);
qui sum rsalmin if year==1994 & qtr==2;
local rsalmin1994 = r(mean);
qui sum rsalmin if year==1995 & qtr==2;
local rsalmin1995 = r(mean);
qui sum rsalmin if year==1996 & qtr==2;
local rsalmin1996 = r(mean);
qui sum rsalmin if year==1997 & qtr==2;
local rsalmin1997 = r(mean);

*** make fake wage data;

** this wage data is multiples of the min wage, going from 0.025 of
   MW to 25 times MW;

local ninds = 1000; * number of fake individuals;

clear all;
set obs `ninds';
gen sal_mw = (_n/`ninds')*25;

*find upper bound of that salary range for merging in schedule;
gen sal_mw_round=round(sal_mw,.01);
gen max_mw=ceil(4*sal_mw_round)/4;

sort max_mw;

*** merge in benefits schedule;
merge max_mw using `d2';
tab _merge;
drop if _merge==2;
drop _merge;

*** enter by hand the benefits for individuals making below the minimum
    wage or above 6 times the minimum wage;

replace avg=80 if max_mw<1;
replace add_yr=0.563 if max_mw<1;

replace avg=13 if max_mw>6;
replace add_yr=2.45 if max_mw>6;

drop sal_mw_round max_mw min_mw;


*** Calculate monthly pension for individuals retiring with average wage;

gen benefit95_avg = sal_mw*(avg/100);
replace benefit95_avg = 25*(avg/100) if sal_mw>25;
gen benefit95_add_yr = sal_mw*(add_yr/100);
replace benefit95_add_yr = 25*(add_yr/100) if sal_mw>25;

gen pension95_10yr = benefit95_avg*`rsalmin1995';
replace pension95_10yr = 1*`rsalmin1995' if pension95_10yr < 1*`rsalmin1995';
gen pension95_15yr = benefit95_avg*`rsalmin1995' + 5*benefit95_add_yr*`rsalmin1995';
replace pension95_15yr = 1*`rsalmin1995' if pension95_15yr < 1*`rsalmin1995';
gen pension95_20yr = benefit95_avg*`rsalmin1995' + 10*benefit95_add_yr*`rsalmin1995';
replace pension95_20yr = 1*`rsalmin1995' if pension95_20yr < 1*`rsalmin1995';
gen pension95_25yr = benefit95_avg*`rsalmin1995' + 15*benefit95_add_yr*`rsalmin1995';
replace pension95_25yr = 1*`rsalmin1995' if pension95_25yr < 1*`rsalmin1995';
gen pension95_30yr = benefit95_avg*`rsalmin1995' + 20*benefit95_add_yr*`rsalmin1995';
replace pension95_30yr = 1*`rsalmin1995' if pension95_30yr < 1*`rsalmin1995';
gen pension95_35yr = benefit95_avg*`rsalmin1995' + 25*benefit95_add_yr*`rsalmin1995';
replace pension95_35yr = 1*`rsalmin1995' if pension95_35yr < 1*`rsalmin1995';
gen pension95_40yr = benefit95_avg*`rsalmin1995' + 30*benefit95_add_yr*`rsalmin1995';
replace pension95_40yr = 1*`rsalmin1995' if pension95_40yr < 1*`rsalmin1995';

gen pension97_10yr = benefit95_avg*`rsalmin1997';
replace pension97_10yr = 1*`rsalmin1997' if pension97_10yr < 1*`rsalmin1997';
gen pension97_15yr = benefit95_avg*`rsalmin1997' + 5*benefit95_add_yr*`rsalmin1997';
replace pension97_15yr = 1*`rsalmin1997' if pension97_15yr < 1*`rsalmin1997';
gen pension97_20yr = benefit95_avg*`rsalmin1997' + 10*benefit95_add_yr*`rsalmin1997';
replace pension97_20yr = 1*`rsalmin1997' if pension97_20yr < 1*`rsalmin1997';
gen pension97_25yr = benefit95_avg*`rsalmin1997' + 15*benefit95_add_yr*`rsalmin1997';
replace pension97_25yr = 1*`rsalmin1997' if pension97_25yr < 1*`rsalmin1997';
gen pension97_30yr = benefit95_avg*`rsalmin1997' + 20*benefit95_add_yr*`rsalmin1997';
replace pension97_30yr = 1*`rsalmin1997' if pension97_30yr < 1*`rsalmin1997';
gen pension97_35yr = benefit95_avg*`rsalmin1997' + 25*benefit95_add_yr*`rsalmin1997';
replace pension97_35yr = 1*`rsalmin1997' if pension97_35yr < 1*`rsalmin1997';
gen pension97_40yr = benefit95_avg*`rsalmin1997' + 30*benefit95_add_yr*`rsalmin1997';
replace pension97_40yr = 1*`rsalmin1997' if pension97_40yr < 1*`rsalmin1997';

** For 1994: same as above, but minimum pension is 90% of min wage;

gen pension94_10yr = benefit95_avg*`rsalmin1994';
replace pension94_10yr = 0.9*`rsalmin1994' if pension94_10yr < 0.9*`rsalmin1994';
gen pension94_15yr = benefit95_avg*`rsalmin1994' + 5*benefit95_add_yr*`rsalmin1994';
replace pension94_15yr = 0.9*`rsalmin1994' if pension94_15yr < 0.9*`rsalmin1994';
gen pension94_20yr = benefit95_avg*`rsalmin1994' + 10*benefit95_add_yr*`rsalmin1994';
replace pension94_20yr = 0.9*`rsalmin1994' if pension94_20yr < 0.9*`rsalmin1994';
gen pension94_25yr = benefit95_avg*`rsalmin1994' + 15*benefit95_add_yr*`rsalmin1994';
replace pension94_25yr = 0.9*`rsalmin1994' if pension94_25yr < 0.9*`rsalmin1994';
gen pension94_30yr = benefit95_avg*`rsalmin1994' + 20*benefit95_add_yr*`rsalmin1994';
replace pension94_30yr = 0.9*`rsalmin1994' if pension94_30yr < 0.9*`rsalmin1994';
gen pension94_35yr = benefit95_avg*`rsalmin1994' + 25*benefit95_add_yr*`rsalmin1994';
replace pension94_35yr = 0.9*`rsalmin1994' if pension94_35yr < 0.9*`rsalmin1994';
gen pension94_40yr = benefit95_avg*`rsalmin1994' + 30*benefit95_add_yr*`rsalmin1994';
replace pension94_40yr = 0.9*`rsalmin1994' if pension94_40yr < 0.9*`rsalmin1994';

** For 1993: same as 1994, but top code is 10X mw;

gen benefit93_avg = sal_mw*(avg/100);
replace benefit93_avg = 10*(avg/100) if sal_mw>10;
gen benefit93_add_yr = sal_mw*(add_yr/100);
replace benefit93_add_yr = 10*(add_yr/100) if sal_mw>10;

gen pension93_10yr = benefit93_avg*`rsalmin1993';
replace pension93_10yr = 0.9*`rsalmin1993' if pension93_10yr < 0.9*`rsalmin1993';
gen pension93_15yr = benefit93_avg*`rsalmin1993' + 5*benefit93_add_yr*`rsalmin1993';
replace pension93_15yr = 0.9*`rsalmin1993' if pension93_15yr < 0.9*`rsalmin1993';
gen pension93_20yr = benefit93_avg*`rsalmin1993' + 10*benefit93_add_yr*`rsalmin1993';
replace pension93_20yr = 0.9*`rsalmin1993' if pension93_20yr < 0.9*`rsalmin1993';
gen pension93_25yr = benefit93_avg*`rsalmin1993' + 15*benefit93_add_yr*`rsalmin1993';
replace pension93_25yr = 0.9*`rsalmin1993' if pension93_25yr < 0.9*`rsalmin1993';
gen pension93_30yr = benefit93_avg*`rsalmin1993' + 20*benefit93_add_yr*`rsalmin1993';
replace pension93_30yr = 0.9*`rsalmin1993' if pension93_30yr < 0.9*`rsalmin1993';
gen pension93_35yr = benefit93_avg*`rsalmin1993' + 25*benefit93_add_yr*`rsalmin1993';
replace pension93_35yr = 0.9*`rsalmin1993' if pension93_35yr < 0.9*`rsalmin1993';
gen pension93_40yr = benefit93_avg*`rsalmin1993' + 30*benefit93_add_yr*`rsalmin1993';
replace pension93_40yr = 0.9*`rsalmin1993' if pension93_40yr < 0.9*`rsalmin1993';


** For 1991-1992: same as 1993, but minimum pension is 80% of mw;

gen pension91_10yr = benefit93_avg*`rsalmin1991';
replace pension91_10yr = 0.8*`rsalmin1991' if pension91_10yr < 0.8*`rsalmin1991';
gen pension91_15yr = benefit93_avg*`rsalmin1991' + 5*benefit93_add_yr*`rsalmin1991';
replace pension91_15yr = 0.8*`rsalmin1991' if pension91_15yr < 0.8*`rsalmin1991';
gen pension91_20yr = benefit93_avg*`rsalmin1991' + 10*benefit93_add_yr*`rsalmin1991';
replace pension91_20yr = 0.8*`rsalmin1991' if pension91_20yr < 0.8*`rsalmin1991';
gen pension91_25yr = benefit93_avg*`rsalmin1991' + 15*benefit93_add_yr*`rsalmin1991';
replace pension91_25yr = 0.8*`rsalmin1991' if pension91_25yr < 0.8*`rsalmin1991';
gen pension91_30yr = benefit93_avg*`rsalmin1991' + 20*benefit93_add_yr*`rsalmin1991';
replace pension91_30yr = 0.8*`rsalmin1991' if pension91_30yr < 0.8*`rsalmin1991';
gen pension91_35yr = benefit93_avg*`rsalmin1991' + 25*benefit93_add_yr*`rsalmin1991';
replace pension91_35yr = 0.8*`rsalmin1991' if pension91_35yr < 0.8*`rsalmin1991';
gen pension91_40yr = benefit93_avg*`rsalmin1991' + 30*benefit93_add_yr*`rsalmin1991';
replace pension91_40yr = 0.8*`rsalmin1991' if pension91_40yr < 0.8*`rsalmin1991';

gen pension92_10yr = benefit93_avg*`rsalmin1992';
replace pension92_10yr = 0.8*`rsalmin1992' if pension92_10yr < 0.8*`rsalmin1992';
gen pension92_15yr = benefit93_avg*`rsalmin1992' + 5*benefit93_add_yr*`rsalmin1992';
replace pension92_15yr = 0.8*`rsalmin1992' if pension92_15yr < 0.8*`rsalmin1992';
gen pension92_20yr = benefit93_avg*`rsalmin1992' + 10*benefit93_add_yr*`rsalmin1992';
replace pension92_20yr = 0.8*`rsalmin1992' if pension92_20yr < 0.8*`rsalmin1992';
gen pension92_25yr = benefit93_avg*`rsalmin1992' + 15*benefit93_add_yr*`rsalmin1992';
replace pension92_25yr = 0.8*`rsalmin1992' if pension92_25yr < 0.8*`rsalmin1992';
gen pension92_30yr = benefit93_avg*`rsalmin1992' + 20*benefit93_add_yr*`rsalmin1992';
replace pension92_30yr = 0.8*`rsalmin1992' if pension92_30yr < 0.8*`rsalmin1992';
gen pension92_35yr = benefit93_avg*`rsalmin1992' + 25*benefit93_add_yr*`rsalmin1992';
replace pension92_35yr = 0.8*`rsalmin1992' if pension92_35yr < 0.8*`rsalmin1992';
gen pension92_40yr = benefit93_avg*`rsalmin1992' + 30*benefit93_add_yr*`rsalmin1992';
replace pension92_40yr = 0.8*`rsalmin1992' if pension92_40yr < 0.8*`rsalmin1992';

** 1989-1990: different benefit structure;
** The top bracket is for 280 pesos per day, which is actually
   0.280 new pesos, which makes everyone fall into the top
   bracket with 35% base rate and 1.25 percent additional;
** Minimum pension if 70% of minimum wage, and pension
   is indexed;

gen benefit89_avg = sal_mw*0.35;
replace benefit89_avg = 10*0.35 if sal_mw>10;
gen benefit89_add_yr = sal_mw*0.0125;
replace benefit89_add_yr = 10*0.0125 if sal_mw>10;

gen pension89_10yr = benefit89_avg*`rsalmin1989';
replace pension89_10yr = 0.7*`rsalmin1989' if pension89_10yr < 0.7*`rsalmin1989';
gen pension89_15yr = benefit89_avg*`rsalmin1989' + 5*benefit89_add_yr*`rsalmin1989';
replace pension89_15yr = 0.7*`rsalmin1989' if pension89_15yr < 0.7*`rsalmin1989';
gen pension89_20yr = benefit89_avg*`rsalmin1989' + 10*benefit89_add_yr*`rsalmin1989';
replace pension89_20yr = 0.7*`rsalmin1989' if pension89_20yr < 0.7*`rsalmin1989';
gen pension89_25yr = benefit89_avg*`rsalmin1989' + 15*benefit89_add_yr*`rsalmin1989';
replace pension89_25yr = 0.7*`rsalmin1989' if pension89_25yr < 0.7*`rsalmin1989';
gen pension89_30yr = benefit89_avg*`rsalmin1989' + 20*benefit89_add_yr*`rsalmin1989';
replace pension89_30yr = 0.7*`rsalmin1989' if pension89_30yr < 0.7*`rsalmin1989';
gen pension89_35yr = benefit89_avg*`rsalmin1989' + 25*benefit89_add_yr*`rsalmin1989';
replace pension89_35yr = 0.7*`rsalmin1989' if pension89_35yr < 0.7*`rsalmin1989';
gen pension89_40yr = benefit89_avg*`rsalmin1989' + 30*benefit89_add_yr*`rsalmin1989';
replace pension89_40yr = 0.7*`rsalmin1989' if pension89_40yr < 0.7*`rsalmin1989';

gen pension90_10yr = benefit89_avg*`rsalmin1990';
replace pension90_10yr = 0.7*`rsalmin1990' if pension90_10yr < 0.7*`rsalmin1990';
gen pension90_15yr = benefit89_avg*`rsalmin1990' + 5*benefit89_add_yr*`rsalmin1990';
replace pension90_15yr = 0.7*`rsalmin1990' if pension90_15yr < 0.7*`rsalmin1990';
gen pension90_20yr = benefit89_avg*`rsalmin1990' + 10*benefit89_add_yr*`rsalmin1990';
replace pension90_20yr = 0.7*`rsalmin1990' if pension90_20yr < 0.7*`rsalmin1990';
gen pension90_25yr = benefit89_avg*`rsalmin1990' + 15*benefit89_add_yr*`rsalmin1990';
replace pension90_25yr = 0.7*`rsalmin1990' if pension90_25yr < 0.7*`rsalmin1990';
gen pension90_30yr = benefit89_avg*`rsalmin1990' + 20*benefit89_add_yr*`rsalmin1990';
replace pension90_30yr = 0.7*`rsalmin1990' if pension90_30yr < 0.7*`rsalmin1990';
gen pension90_35yr = benefit89_avg*`rsalmin1990' + 25*benefit89_add_yr*`rsalmin1990';
replace pension90_35yr = 0.7*`rsalmin1990' if pension90_35yr < 0.7*`rsalmin1990';
gen pension90_40yr = benefit89_avg*`rsalmin1990' + 30*benefit89_add_yr*`rsalmin1990';
replace pension90_40yr = 0.7*`rsalmin1990' if pension90_40yr < 0.7*`rsalmin1990';


foreach yr in 1989 1990 1991 1993 1994 1995 1997
{;


  if `yr'==1989 local years "1989";
  if `yr'==1990 local years "1990";
  if `yr'==1991 local years "1991";
  if `yr'==1993 local years "1993";
  if `yr'==1994 local years "1994";
  if `yr'==1995 local years "1995";
  if `yr'==1997 local years "1997";

  gen rsal=sal_mw*`rsalmin`yr'';

  local yr2=`yr'-1900;

  graph twoway scatter pension`yr2'_10yr pension`yr2'_20yr pension`yr2'_30yr rsal
    if rsal<=800,
    msymbol(i i i)
    connect(l l l) clwidth(medthick medthick medthick)
    clcolor(blue green red) sort
    title("`years'", size(medsmall))
    ytitle("real pension (2002 pesos/day)")
    legend(label(1 "10 yrs conts.") label(2 "20 yrs conts.") label(3 "30 yrs conts.")
           cols(3) size(vsmall))
    xtitle("final avg wage (2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2))
    ylab(0(100)700) xlab(0(200)800)
    saving(${figs}fpenben_`yr', replace);

 graph export ${figs}fpenben_`yr'.ps, logo(off) mag(150) orientation(landscape) replace;

 drop rsal;
};

grc1leg ${figs}fpenben_1990.gph
	${figs}fpenben_1993.gph
	${figs}fpenben_1997.gph, scheme(s1mono) iscale(*.8)
    rows(1) cols(3) ycommon title("A. Value of pension by wage, ages 60-65", size(small))
    saving(${figs}fpenben, replace);

  graph display, xsize(10) ysize(7.5);
  graph export ${figs}fpenben.ps, logo(off) mag(100) orientation(landscape) replace;


****************** graphs with percentiles **************;

*  program calculates the salary percentiles in each year, so we can create
   graphs based on the individual making the X%-tile;

*** get benefit files ready;

clear all;
use ${work}imss_benefits;
sort year qtr;
tab qtr;
save `d1', replace;

clear all;
use ${raw}imss_benefits_sched_ev_2011_09_26.dta;
sort max_mw;
save `d2', replace;


*** get minimum wages file ready;

* Here I am just keeping minimum wages for zone A (Mexico City). May
  want to come back to this;

clear all;
use ${work}minwages_wtopcodes;
keep if zona=="A";
keep year qtr salmin cpi;
sort year qtr;
tab qtr;
save `d3', replace;

*save local for 1997 minimum wage to use in calculating post-97 benefits;

qui sum salmin if year==1997;
local salmin97 = r(mean);
qui sum cpi if year==1997;
local cpi97 = r(mean);


** dont need to run each time;

forval i=1985/2003
{;

 di "year = `i'";

 use nss age rsal7 cpi male if male==1 using ${work}imss_forhh_`i'6, clear;

 *convert top coded real salary to top coded nominal salary;
 gen sal = rsal7 * (cpi/100);
 drop rsal7;

 pctile sal`i'p = sal if male==1, n(100) g(pctile);
 pctile sal`i'60p = sal if inrange(age,60,65) & male==1, n(100) g(pctile2);

 rename sal`i'p sal`i';
 rename sal`i'60p sal`i'_60;

 drop pctile2 sal age nss cpi;

 drop if pctile==.;

 sort pctile;

 tempfile d`i'_m;

 save `d`i'_m';

 * I am using the IMSS data for our sample to create the percentiles;
 use nss age rsal7 cpi male if male==0 using ${work}imss_forhh_`i'6, clear;

 *convert top coded real salary to top coded nominal salary;
 gen sal = rsal7 * (cpi/100);
 drop rsal7;

 pctile sal`i'p = sal if male==0, n(100) g(pctile);
 pctile sal`i'60p = sal if inrange(age,60,65) & male==0, n(100) g(pctile2);

 rename sal`i'p sal`i';
 rename sal`i'60p sal`i'_60;

 drop pctile2 sal age nss cpi;

 drop if pctile==.;

 sort pctile;

 tempfile d`i'_f;

 save `d`i'_f';

};


forval i=1988/2003
{;

 di "year = `i'";

 * men;
 use age rsal7 cpi male year qtr factor work_imss work_ft_imss
     if male==1 & year==`i' & qtr==2 using ${work}eneu_forimss_19882003, clear;
 drop year qtr;

 keep if work_ft_imss==1;

 * convert top coded real salary to top coded nominal salary;
 gen sal = rsal7 * (cpi/100);
 drop rsal7;

 pctile sal`i'p = sal if male==1 [aw=factor], n(100) g(pctile);
 pctile sal`i'60p = sal if inrange(age,60,65) & male==1 [aw=factor], n(100) g(pctile2);

 rename sal`i'p sal`i';
 rename sal`i'60p sal`i'_60;

 drop pctile2 sal age cpi;

 drop if pctile==.;

 sort pctile;

 tempfile d`i'_m_eneu;

 save `d`i'_m_eneu';

 ** women;
 use age rsal7 cpi male year qtr factor work_imss work_ft_imss
     if male==0 & year==`i' & qtr==2 using ${work}eneu_forimss_19882003, clear;

 drop year qtr;

 keep if work_ft_imss==1;

 *convert top coded real salary to top coded nominal salary;
 gen sal = rsal7 * (cpi/100);
 drop rsal7;

 pctile sal`i'p = sal if male==0 [aw=factor], n(100) g(pctile);
 pctile sal`i'60p = sal if inrange(age,60,65) & male==0 [aw=factor], n(100) g(pctile2);

 rename sal`i'p sal`i';
 rename sal`i'60p sal`i'_60;

 drop pctile2 sal age cpi;

 drop if pctile==.;

 sort pctile;

 tempfile d`i'_f_eneu;

 save `d`i'_f_eneu';

};


foreach m in m f {;

use `d1985_`m'', clear;

merge pctile using `d1986_`m'' `d1987_`m''  `d1988_`m''  `d1989_`m''  `d1990_`m''  `d1991_`m''  `d1992_`m''
	 `d1993_`m''  `d1994_`m'' `d1995_`m'' `d1996_`m'' `d1997_`m'' `d1998_`m'' `d1999_`m'' `d2000_`m'' `d2001_`m''
	  `d2002_`m'' `d2003_`m'';

drop _merge*;

gen mean1989 = (sal1985 + sal1986 + sal1987 + sal1988 + sal1989)/5;
gen mean1990 = (sal1990 + sal1986 + sal1987 + sal1988 + sal1989)/5;
gen mean1991 = (sal1990 + sal1991 + sal1987 + sal1988 + sal1989)/5;
gen mean1992 = (sal1990 + sal1991 + sal1992 + sal1988 + sal1989)/5;
gen mean1993 = (sal1990 + sal1991 + sal1992 + sal1993 + sal1989)/5;
gen mean1994 = (sal1990 + sal1991 + sal1992 + sal1993 + sal1994)/5;
gen mean1995 = (sal1995 + sal1991 + sal1992 + sal1993 + sal1994)/5;
gen mean1996 = (sal1995 + sal1996 + sal1992 + sal1993 + sal1994)/5;
gen mean1997 = (sal1995 + sal1996 + sal1997 + sal1993 + sal1994)/5;
gen mean1998 = (sal1995 + sal1996 + sal1997 + sal1998 + sal1994)/5;
gen mean1999 = (sal1995 + sal1996 + sal1997 + sal1998 + sal1999)/5;
gen mean2000 = (sal2000 + sal1996 + sal1997 + sal1998 + sal1999)/5;
gen mean2001 = (sal2000 + sal2001 + sal1997 + sal1998 + sal1999)/5;
gen mean2002 = (sal2000 + sal2001 + sal2002 + sal1998 + sal1999)/5;
gen mean2003 = (sal2000 + sal2001 + sal2002 + sal2003 + sal1999)/5;

gen mean1988 = (sal1985 + sal1986 + sal1987 + sal1988 + sal1985)/5;
gen mean1987 = (sal1985 + sal1986 + sal1987 + sal1985 + sal1985)/5;
gen mean1986 = (sal1985 + sal1986 + sal1985 + sal1985 + sal1985)/5;
gen mean1985 = (sal1985 + sal1985 + sal1985 + sal1985 + sal1985)/5;

gen mean60_1989 = (sal1985_60 + sal1986_60 + sal1987_60 + sal1988_60 + sal1989_60)/5;
gen mean60_1990 = (sal1990_60 + sal1986_60 + sal1987_60 + sal1988_60 + sal1989_60)/5;
gen mean60_1991 = (sal1990_60 + sal1991_60 + sal1987_60 + sal1988_60 + sal1989_60)/5;
gen mean60_1992 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1988_60 + sal1989_60)/5;
gen mean60_1993 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1989_60)/5;
gen mean60_1994 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1995 = (sal1995_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1996 = (sal1995_60 + sal1996_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1997 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1998 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1994_60)/5;
gen mean60_1999 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2000 = (sal2000_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2001 = (sal2000_60 + sal2001_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2002 = (sal2000_60 + sal2001_60 + sal2002_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2003 = (sal2000_60 + sal2001_60 + sal2002_60 + sal2003_60 + sal1999_60)/5;

gen mean60_1988 = (sal1985_60 + sal1986_60 + sal1987_60 + sal1988_60 + sal1985_60)/5;
gen mean60_1987 = (sal1985_60 + sal1986_60 + sal1987_60 + sal1985_60 + sal1985_60)/5;
gen mean60_1986 = (sal1985_60 + sal1986_60 + sal1985_60 + sal1985_60 + sal1985_60)/5;
gen mean60_1985 = (sal1985_60 + sal1985_60 + sal1985_60 + sal1985_60 + sal1985_60)/5;

keep mean* pctile;

reshape long mean mean60_, i(pctile) j(year);
rename mean60_ mean60;

sort year pctile;

tab year;

save ${tmp}sal_pct_`m'_imss, replace;


use `d1988_`m'_eneu', clear;

merge pctile using `d1989_`m'_eneu'  `d1990_`m'_eneu'  `d1991_`m'_eneu'  `d1992_`m'_eneu'
	 `d1993_`m'_eneu'  `d1994_`m'_eneu' `d1995_`m'_eneu' `d1996_`m'_eneu'
	 `d1997_`m'_eneu' `d1998_`m'_eneu' `d1999_`m'_eneu' `d2000_`m'_eneu' `d2001_`m'_eneu'
	  `d2002_`m'_eneu' `d2003_`m'_eneu';

drop _merge*;

gen mean1992 = (sal1990 + sal1991 + sal1992 + sal1988 + sal1989)/5;
gen mean1993 = (sal1990 + sal1991 + sal1992 + sal1993 + sal1989)/5;
gen mean1994 = (sal1990 + sal1991 + sal1992 + sal1993 + sal1994)/5;
gen mean1995 = (sal1995 + sal1991 + sal1992 + sal1993 + sal1994)/5;
gen mean1996 = (sal1995 + sal1996 + sal1992 + sal1993 + sal1994)/5;
gen mean1997 = (sal1995 + sal1996 + sal1997 + sal1993 + sal1994)/5;
gen mean1998 = (sal1995 + sal1996 + sal1997 + sal1998 + sal1994)/5;
gen mean1999 = (sal1995 + sal1996 + sal1997 + sal1998 + sal1999)/5;
gen mean2000 = (sal2000 + sal1996 + sal1997 + sal1998 + sal1999)/5;
gen mean2001 = (sal2000 + sal2001 + sal1997 + sal1998 + sal1999)/5;
gen mean2002 = (sal2000 + sal2001 + sal2002 + sal1998 + sal1999)/5;
gen mean2003 = (sal2000 + sal2001 + sal2002 + sal2003 + sal1999)/5;

gen mean1991 = (sal1988 + sal1989 + sal1990 + sal1991 + sal1988)/5;
gen mean1990 = (sal1988 + sal1989 + sal1990 + sal1988 + sal1988)/5;
gen mean1989 = (sal1988 + sal1989 + sal1988 + sal1988 + sal1988)/5;
gen mean1988 = (sal1988 + sal1988 + sal1988 + sal1988 + sal1988)/5;

gen mean60_1992 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1988_60 + sal1989_60)/5;
gen mean60_1993 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1989_60)/5;
gen mean60_1994 = (sal1990_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1995 = (sal1995_60 + sal1991_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1996 = (sal1995_60 + sal1996_60 + sal1992_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1997 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1993_60 + sal1994_60)/5;
gen mean60_1998 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1994_60)/5;
gen mean60_1999 = (sal1995_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2000 = (sal2000_60 + sal1996_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2001 = (sal2000_60 + sal2001_60 + sal1997_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2002 = (sal2000_60 + sal2001_60 + sal2002_60 + sal1998_60 + sal1999_60)/5;
gen mean60_2003 = (sal2000_60 + sal2001_60 + sal2002_60 + sal2003_60 + sal1999_60)/5;

gen mean60_1991 = (sal1988_60 + sal1989_60 + sal1990_60 + sal1991_60 + sal1988_60)/5;
gen mean60_1990 = (sal1988_60 + sal1989_60 + sal1990_60 + sal1988_60 + sal1988_60)/5;
gen mean60_1989 = (sal1988_60 + sal1989_60 + sal1988_60 + sal1988_60 + sal1988_60)/5;
gen mean60_1988 = (sal1988_60 + sal1988_60 + sal1988_60 + sal1988_60 + sal1988_60)/5;

keep mean* pctile;

reshape long mean mean60_, i(pctile) j(year);
rename mean60_ mean60;

sort year pctile;

tab year;

save ${tmp}sal_pct_`m'_eneu, replace;

};


*** make figures;

foreach sample in imss eneu {;
	foreach sex in m f {;

		if "`sample'"=="imss" local letter B.;
		if "`sample'"=="eneu" local letter C.;

		if "`sample'"=="imss" local source IMSS;
		if "`sample'"=="eneu" local source ENEU;

		use ${tmp}sal_pct_`sex'_`sample', clear;

                * here we use percentiles for 60-65 year olds;
		rename mean60 sal;
		gen qtr=2;

		*** merge in benefit information;

		sort year qtr;
		merge year qtr using `d1', uniqusing;
		tab _merge;
		tab year if _merge==1;
		tab year if _merge==2;
		keep if _merge==3;
		drop _merge;

		*** merge in minimum wages, cpi;

		sort year qtr;
		merge year qtr using `d3', uniqusing;
		tab _merge;
		keep if _merge==3;
		drop _merge;

		gen rsal = sal/(cpi/100);
		gen rsalmin = salmin/(cpi/100);

		*calculate salary relative to minimum wage, then find upper
		bound of that salary range for merging in schedule;
		gen sal_mw=sal/salmin;
		gen sal_mw_round=round(sal_mw,.01);
		gen max_mw=ceil(4*sal_mw_round)/4;

		sort max_mw;

		*** merge in benefits schedule;
		merge max_mw using `d2';
		tab _merge;
		drop if _merge==2;
		drop _merge;

		*** enter by hand the benefits for individuals making below the minimum
		    wage or above 6 times the minimum wage;

		replace avg=80 if max_mw<1;
		replace add_yr=0.563 if max_mw<1;

		replace avg=13 if max_mw>6;
		replace add_yr=2.45 if max_mw>6;

		drop sal_mw_round max_mw min_mw;

		gen benefit_avg = sal*(avg/100);
		replace benefit_avg = 25*salmin*(avg/100) if year>=1994 & sal_mw>25;
		replace benefit_avg = 10*salmin*(avg/100) if inrange(year,1991,1993) & sal_mw>10;
		replace benefit_avg = sal*.35 if year<=1991;
		replace benefit_avg = 10*salmin*.35 if year<=1991 & sal_mw>=10;
		gen benefit_add_yr = sal*(add_yr/100);
		replace benefit_add_yr = 25*salmin*(add_yr/100) if year>=1994 & sal_mw>25;
		replace benefit_add_yr = 25*salmin*(add_yr/100) if inrange(year,1991,1993) & sal_mw>10;
		replace benefit_add_yr = 10*salmin*(avg/100) if inrange(year,1991,1993) & sal_mw>10;
		replace benefit_add_yr = sal*.0125 if year<=1991 & sal_mw<10;
		replace benefit_add_yr = 10*salmin*.0125 if year<=1991 & sal_mw>=10;

		gen pension_10yr = benefit_avg;
		replace pension_10yr = (min_p_rate/100)*salmin if pension_10yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_15yr = benefit_avg + 5*benefit_add_yr;
		replace pension_15yr = (min_p_rate/100)*salmin if pension_15yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_20yr = benefit_avg + 10*benefit_add_yr;
		replace pension_20yr = (min_p_rate/100)*salmin if pension_20yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_25yr = benefit_avg + 15*benefit_add_yr;
		replace pension_25yr = (min_p_rate/100)*salmin if pension_25yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_30yr = benefit_avg + 20*benefit_add_yr;
		replace pension_30yr = (min_p_rate/100)*salmin if pension_30yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_35yr = benefit_avg + 25*benefit_add_yr;
		replace pension_35yr = (min_p_rate/100)*salmin if pension_35yr < (min_p_rate/100)*salmin & year>=1989;
		gen pension_40yr = benefit_avg + 30*benefit_add_yr;
		replace pension_40yr = (min_p_rate/100)*salmin if pension_40yr < (min_p_rate/100)*salmin & year>=1989;

		forval yr=10(5)40 {;
			replace pension_`yr'yr=pension_`yr'yr / (cpi/100);
		};


		forval yr=1989/2004
		{;

		  graph twoway scatter pension_10yr pension_20yr pension_30yr pctile if year==`yr',
		    msymbol(i i i)
		    connect(l l l) clwidth(medthick medthick medthick)
		    clcolor(blue green red) sort
		    title("`yr'", size(medsmall))
		    ytitle("real pension (2002 pesos/day)")
		    legend(label(1 "10 yrs conts.") label(2 "20 yrs conts.")
                           label(3 "30 yrs conts.") rows(1) size(vsmall))
		    xtitle("final average wage percentile") scheme(s1mono) xscale(titlegap(2))
		    yscale(titlegap(2))
		    ylab(0(50)300) xlab(0(20)100)
		    saving(${figs}fpenbenptile_`yr'_`sex'_`sample', replace);

		 graph export ${figs}fpenbenptile_`yr'_`sex'_`sample'.ps, logo(off) mag(150)
                    orientation(landscape) replace;
		 
		};

		grc1leg ${figs}fpenbenptile_1990_`sex'_`sample'.gph
			${figs}fpenbenptile_1993_`sex'_`sample'.gph
			${figs}fpenbenptile_1997_`sex'_`sample'.gph, scheme(s1mono) iscale(*.8)
                            legendfrom(${figs}fpenbenptile_1990_`sex'_`sample'.gph)
		    rows(1) cols(3) title("`letter' Value of pension by `source' wage percentile, ages 60-65",
                                          size(small))
		    saving(${figs}fpenbenptile_`sex'_`sample', replace);

		graph display, xsize(9) ysize(7);
		graph export ${figs}fpenbenptile_`sex'_`sample'.ps, logo(off) mag(100) orientation(landscape) replace;

	};
};

grc1leg ${figs}fpenben.gph
	${figs}fpenbenptile_m_imss.gph
	${figs}fpenbenptile_m_eneu.gph, scheme(s1mono)
	rows(3) cols(1)
	saving(${figs}fpensions_male1, replace);

graph display, xsize(7) ysize(10);
graph export ${figs}fpensions_male1.ps, logo(off) mag(100) replace;

grc1leg ${figs}fpenben.gph
	${figs}fpenbenptile_f_imss.gph
	${figs}fpenbenptile_f_eneu.gph, scheme(s1mono)
	rows(3) cols(1)
	saving(${figs}fpensions_male0, replace);

graph display, xsize(7) ysize(10);
graph export ${figs}fpensions_male0.ps, logo(off) mag(100) orientation(portrait) replace;

**********************************************************************************;
********* Figs 2 & 3 (fhistsex_yr): histograms by sex for particular year(s) **********;
**********************************************************************************;

 **** loop over year-sex cells (aggregate across ages, metro areas) ****;

 *only look at quarter 2 for now;

 foreach yr in 1990
 {;
  foreach qtr in 2
  {;

   local month=`qtr'*3;
   di "year = `yr', quarter = `qtr', month = `month'";
   clear;
   use ${work}imss_forhh_`yr'`month';

   forval male = 0/1
   {;

    di "year = `yr', quarter = `qtr', month = `month', male = `male'";

    preserve;

    ***** get IMSS data ready ****;

    keep if male == `male';

    save `d1', replace;

    ***** get ENEU data ready ****;

    clear;
    use ${work}eneu_forimss_19882003;

    keep if year==`yr' & male == `male' & work_ft_imss == 1;

     keep if qtr == `qtr';

    tab age_cat2, missing;

    *find and save min wages;
    tab rsalmin if qtr==`qtr', matrow(salmin);
    local salmin1=salmin[1,1];
    local salmin2=salmin[2,1];
    local salmin3=salmin[3,1];

    **** stack IMSS and ENEU data ****;

    append using `d1';

    **** make histograms ***;

    twoway (histogram rsal7 if imss==1, width(5) start(0) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7 if eneu==1 & work_ft_imss==1 [fw=factor],
        width(5) start(0)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(0(50)550) ytitle("fraction of sample") ylab(0(0.05)0.15)
        legend(label(1 "IMSS admin. records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsex_yr`yr'_`qtr'_`male'_n, replace);

    graph export ${figs}fhistsex_yr`yr'_`qtr'_`male'_n.ps,
          logo(off) mag(100) orientation(landscape) replace;

    twoway (histogram rsal7 if imss==1, width(2) start(30) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7 if eneu==1 & work_ft_imss==1 [fw=factor],
        width(2) start(30)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction)
        if inrange(rsal7,30,200),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(40(20)200) ytitle("fraction of sample") ylab(0(0.05)0.15)
        legend(label(1 "IMSS admin. records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsex_yr`yr'_`qtr'_`male'_n_low, replace);

    graph export ${figs}fhistsex_yr`yr'_`qtr'_`male'_n_low.ps, logo(off)
          mag(100) orientation(landscape) replace;

    restore;

   };
  };
 };


****************************************************************************************;
***** Fig 4 (fhistsexfs3_yr): histograms by sex and firm size for particular year(s) *******;
****************************************************************************************;

 **** loop over year-sex cells (aggregate across ages, metro areas) ****;

 *only look at quarter 2 for now;

 foreach yr in 1990
 {;
  foreach qtr in 2
  {;
   forval male = 0/1
   {;

    di "year = `yr', quarter = `qtr', male = `male'";

    ***** get IMSS data ready ****;
    clear;
    local month=`qtr'*3;
    use ${work}imss_forhh_`yr'`month';
    keep if male == `male';

    save `d1', replace;

    ***** get ENEU data ready ****;

    clear;
    use ${work}eneu_forimss_19882003;

    keep if year==`yr' & male == `male' & work_ft_imss == 1;

    keep if qtr == `qtr';

    tab age_cat2, missing;

    *find and save min wages;
    tab rsalmin if qtr==`qtr', matrow(salmin);
    local salmin1=salmin[1,1];
    local salmin2=salmin[2,1];
    local salmin3=salmin[3,1];

    **** stack IMSS and ENEU data ****;

    append using `d1';

    **** make histograms ***;

    forval firmsize3 = 1/5
    {;

     di "year = `yr', quarter = `qtr', month = `month', male = `male', firmsize3=`firmsize3'";

     if `firmsize3' == 1 local firmsize3label "1-10 employees";
     if `firmsize3' == 2 local firmsize3label "11-50 employees";
     if `firmsize3' == 3 local firmsize3label "51-100 employees";
     if `firmsize3' == 4 local firmsize3label "101-250 employees";
     if `firmsize3' == 5 local firmsize3label ">250 employees";

     preserve;

     keep if firmsize3==`firmsize3';

     twoway (histogram rsal7 if imss==1, width(5) start(0) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7 if eneu==1 & work_ft_imss==1 [fw=factor],
        width(5) start(0)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        title("`firmsize3label'")
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(0(50)550) ytitle("fraction of sample") ylab(0(0.05)0.25)
        legend(label(1 "IMSS records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(1) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10)
        ysize(7.5)
        scale(1)
        nodraw
        saving(${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs`firmsize3'_n, replace);

     twoway (histogram rsal7 if imss==1, width(2) start(30) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7 if eneu==1 & work_ft_imss==1 [fw=factor],
        width(2) start(30)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction)
        if inrange(rsal7,30,200),
        title("`firmsize3label'")
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(40(20)200)
        ylab(0(0.05)0.25)
        ytitle("fraction of sample")
        legend(label(1 "IMSS records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(1) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10)
        ysize(7.5)
        scale(1)
        nodraw
        saving(${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs`firmsize3'_n_low, replace);

     restore;

   };

    grc1leg ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs1_n_low.gph
        ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs2_n_low.gph
        ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs3_n_low.gph
        ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs4_n_low.gph
        ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_fs5_n_low.gph,
        scheme(s1mono) iscale(*.8)
        rows(2) cols(3) pos(4) ring(0)
        saving(${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_n_low, replace);

    graph display, xsize(10) ysize(7.5);
    graph export ${figs}fhistsexfs3_yr`yr'_`qtr'_`male'_n_low.ps, logo(off) mag(100)
       orientation(landscape) replace;

   };
  };
 };


*****************************************************************************************************;
***** Fig 5 (fldiffmednetdev): log diff wage gap medians (net wage in IMSS), dev from metro-year means ********;
*****************************************************************************************************;

local male="1";

*forval male = 0/1
*{;

clear;
use ${work}cellpqymetro_age_cat2_19882003;

keep if qtr==2;
keep if male==`male';

gen ldiffmednet_ft_imss = log(rsal7_eneu_ft_imss_med/rsal7net_imss_med);
gen ldiffmednet_imss = log(rsal7_eneu_imss_med/rsal7net_imss_med);
gen ldiffmednet_perm = log(rsal7_eneu_med_perm_imss/rsal7net_imss_med);

gen str6 metroyear = string(metro_area)+string(year);

areg ldiffmednet_ft_imss [pw=empl_imss], absorb(metroyear);

predict ldiffmednet_ft_imss_dev, residuals;

sort year age_cat2;

collapse (mean) ldiffmednet_ft_imss_dev, by(year age_cat2);

forvalues age_cat2 = 1/5
{;
  gen ldiffmednet_ft_imss_dev`age_cat2' = ldiffmednet_ft_imss_dev if age_cat2==`age_cat2';
};


graph twoway scatter ldiffmednet_ft_imss_dev1-ldiffmednet_ft_imss_dev5 year,
    msymbol(i i i i i i) connect(l l l l l)
    clwidth(medthick medthick medthick medthick medthick)
    clcolor(blue green red orange purple) sort
    ytitle("wage gap (medians), deviated from metro-year means")
    xtitle("Year")
    scheme(s1mono)
    xscale(titlegap(2))
    yscale(titlegap(2) range(-.2 .2))
    xlab(1988(2)2002)
    ylab(-.2(.1).2)
    xline(1995.4, lwidth(medthick) lpattern(dash) lcolor(gs4))
    xline(1997.0, lwidth(medthin) lpattern(solid) lcolor(gs4))
    legend(label(1 "age 16-25") label(2 "age 26-35") label(3 "age 36-45")
           label(4 "age 46-55") label(5 "age 56-65") pos(8) ring(0) col(2)
           size(small) region(lstyle(none)) width(50))
    xsize(10)
    ysize(7.5)
    scale(.6)
    saving(${figs}fldiffmednetdev_ft_imss_male`male'_y, replace);

graph export ${figs}fldiffmednetdev_ft_imss_male`male'_y.ps, logo(off) mag(100)
      orientation(landscape) lmargin(.1) replace;

****************************************************************************;
****************************************************************************;
***************** Published appendix figures *******************************;
****************************************************************************;
****************************************************************************;


****************************************************************************;
******** Fig A1 (fcontr_summary): employer contribution schedules, by year ******;
****************************************************************************;

*** get employer contributions file ready;

clear all;
use ${work}imss_employer_contrib;
sort year qtr;
foreach var of varlist sem-limit_cpda {;
	gen `var'_lead1yr = `var'[_n+4];
};
save `d1', replace;

*** get minimum wages file ready;

clear all;
use ${work}minwages_wtopcodes;
keep if zona=="A";
keep year qtr salmin salmindf cpi;
sort year qtr;
save `d2', replace;

*** make fake wage data;

local fyr=1985;
local lyr=2005;
local nyrs = `lyr'-`fyr'+1;
local ninds = 1000; * number of fake individuals;
local nobs = `nyrs'*`ninds';
local highestrsalmin = 100.9658;
local lowestrsalmin = 37.87585;

clear all;
set obs `nobs';
gen year = ceil(_n/`ninds')+`fyr'-1;
gen qtr = 3;
bysort year: gen rsal = (_n/`ninds')*(floor(`highestrsalmin')*30);

keep year qtr rsal;

*** merge in employer contribution percentages;

sort year qtr;
merge year qtr using `d1', uniqusing;
tab _merge;
keep if _merge==3;
drop _merge;

*** merge in minimum wages, cpi;

sort year qtr;
merge year qtr using `d2', uniqusing;
tab _merge;
keep if _merge==3;
drop _merge;

gen sal = rsal*(cpi/100);
gen rsalmin = salmin/(cpi/100);

* Note: here just assume we are in zone A (Mexico City);

* Note: the topcode for cpda (INFONAVIT) depends on local minimum
  wage, not Mexico City minimum wage;

* this variable is the value of the employer contribution, from contributions_imss.do;
gen imss_emp_contrib = (

                    (sal<=limit_sem*salmindf)*(sem/100)*sal +
                    (sal>limit_sem*salmindf)*(sem/100)*limit_sem*salmindf +
                    (sal<=limit_sem*salmindf)*(sem_d/100)*sal +
                    (sal>limit_sem*salmindf)*(sem_d/100)*limit_sem*salmindf +

                    (sal<=limit_siv*salmindf)*(sivcm/100)*sal +
                    (sal>limit_siv*salmindf)*(sivcm/100)*limit_siv*salmindf +
                    (sal<=limit_siv*salmindf)*(siv/100)*sal +
                    (sal>limit_siv*salmindf)*(siv/100)*limit_siv*salmindf +

                    (sal<=limit_srcv_r*salmindf)*(srcv_r/100)*sal +
                    (sal>limit_srcv_r*salmindf)*(srcv_r/100)*limit_srcv_r*salmindf +

                    (sal<=limit_srcv_c*salmindf)*(srcv_c/100)*sal +
                    (sal>limit_srcv_c*salmindf)*(srcv_c/100)*limit_srcv_c*salmindf +

                    (sal<=limit_sgps*salmindf)*(sgps/100)*sal +
                    (sal>limit_sgps*salmindf)*(sgps/100)*limit_sgps*salmindf +

                    (sal<=limit_cpda*salmin)*(cpda/100)*sal +
                    (sal>limit_cpda*salmin)*(cpda/100)*limit_cpda*salmin +

                    (sal<=limit_siv*salmindf)*(aport_esp/100)*sal +
                    (sal>limit_siv*salmindf)*(aport_esp/100)*limit_siv*salmindf +

                    (sal<=limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(sal-3*salmindf) +
                    (sal>limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(limit_sem*salmindf-3*salmindf) +

                    (sem_fija/100)*salmindf

                    );

gen imss_emp_contrib_pred1yr = (

                    (sal<=limit_sem_lead1yr*salmindf)*(sem_lead1yr/100)*sal +
                    (sal>limit_sem_lead1yr*salmindf)*(sem_lead1yr/100)*limit_sem_lead1yr*salmindf +
                    (sal<=limit_sem_lead1yr*salmindf)*(sem_d_lead1yr/100)*sal +
                    (sal>limit_sem_lead1yr*salmindf)*(sem_d_lead1yr/100)*limit_sem_lead1yr*salmindf +

                    (sal<=limit_siv_lead1yr*salmindf)*(sivcm_lead1yr/100)*sal +
                    (sal>limit_siv_lead1yr*salmindf)*(sivcm_lead1yr/100)*limit_siv_lead1yr*salmindf +
                    (sal<=limit_siv_lead1yr*salmindf)*(siv_lead1yr/100)*sal +
                    (sal>limit_siv_lead1yr*salmindf)*(siv_lead1yr/100)*limit_siv_lead1yr*salmindf +

                    (sal<=limit_srcv_r_lead1yr*salmindf)*(srcv_r_lead1yr/100)*sal +
                    (sal>limit_srcv_r_lead1yr*salmindf)*(srcv_r_lead1yr/100)*limit_srcv_r_lead1yr*salmindf +

                    (sal<=limit_srcv_c_lead1yr*salmindf)*(srcv_c_lead1yr/100)*sal +
                    (sal>limit_srcv_c_lead1yr*salmindf)*(srcv_c_lead1yr/100)*limit_srcv_c_lead1yr*salmindf +

                    (sal<=limit_sgps_lead1yr*salmindf)*(sgps_lead1yr/100)*sal +
                    (sal>limit_sgps_lead1yr*salmindf)*(sgps_lead1yr/100)*limit_sgps_lead1yr*salmindf +

                    (sal<=limit_cpda_lead1yr*salmin)*(cpda_lead1yr/100)*sal +
                    (sal>limit_cpda_lead1yr*salmin)*(cpda_lead1yr/100)*limit_cpda_lead1yr*salmin +

                    (sal<=limit_siv*salmindf)*(aport_esp/100)*sal +
                    (sal>limit_siv*salmindf)*(aport_esp/100)*limit_siv*salmindf +

                    (sal<=limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(sal-3*salmindf) +
                    (sal>limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(limit_sem*salmindf-3*salmindf) +

                    (sem_fija_lead1yr/100)*salmindf

                    );

gen imss_emp_contrib_pct = (imss_emp_contrib / sal)*100;
gen imss_emp_contrib_pct_pred1yr = (imss_emp_contrib_pred1yr / sal)*100;

gen topcode=.;
replace topcode=salmindf*10 if year<1993;
replace topcode=salmindf*10 if year==1993 & qtr<=2;
replace topcode=salmindf*18 if year==1993 & qtr>2;
replace topcode=salmindf*25 if year>1994;

gen sal_mw = sal / salmindf;

save `d3', replace;

**** make graphs;

local i = 1985;
while `i'<=2005 {;
                 gen imss_emp_contrib_pct_`i' = imss_emp_contrib_pct if year==`i';
                 label variable imss_emp_contrib_pct_`i' "`i'";
                 local i=`i'+1;
               };

gen tot_emp_contrib = (imss_emp_contrib_pct/100) * rsal;
gen tot_emp_contrib_pred1yr = (imss_emp_contrib_pct_pred1yr/100) * rsal;

gen tot_emp_contrib_sal_mw = (imss_emp_contrib_pct/100) * sal_mw;

local i = 1985;
while `i'<=2005 {;
                 gen tot_emp_contrib_`i' = tot_emp_contrib if year==`i';
                 label variable tot_emp_contrib_`i' "`i'";

                 local i=`i'+1;
               };

local i = 1985;
while `i'<=2005 {;
                 gen tot_emp_contrib_sal_mw_`i' = tot_emp_contrib_sal_mw if year==`i';
                 label variable tot_emp_contrib_sal_mw_`i' "`i'";

                 local i=`i'+1;
               };

sort rsal year;
*calculate changes from year to year;
local i = 1985;
while `i'<=2004 {;
				 local j=`i'+1;
                 by rsal: gen imss_emp_contrib_pct_`i'_`j' =
                  imss_emp_contrib_pct - imss_emp_contrib_pct if year==`i' ;
                 label variable imss_emp_contrib_pct_`i'_`j' "`j' - `i'";
                 local i=`i'+1;
                };

*calculate changes from year to year;
local i = 1985;
while `i'<=2004 {;
				 local j=`i'+1;
                 by rsal: gen tot_emp_contrib_`i'_`j' =
                  tot_emp_contrib_pred1yr - tot_emp_contrib if year==`i' ;
                 label variable tot_emp_contrib_`i'_`j' "`j' - `i'";
                 local i=`i'+1;
                };



foreach year1 in 1988 1992 1996 2000
{;
 forvalues i = 2/4
 {;
  local year`i' = `year1'+`i';
 };
 local year1a = `year1'+1;
 local year3a = `year3'+1;

 * absolute terms;

 graph twoway scatter tot_emp_contrib_`year1'-tot_emp_contrib_`year3'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(50)200)
    saving(${figs}fcontr`year1'_`year3', replace);

 *graph export ${figs}fcontr`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter tot_emp_contrib_`year1'-tot_emp_contrib_`year3'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(5)30)
    saving(${figs}fcontrlow`year1'_`year3', replace);

 *graph export ${figs}fcontrlow`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * absolute terms relative to min wage;

 graph twoway scatter tot_emp_contrib_sal_mw_`year1'-tot_emp_contrib_sal_mw_`year3'
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (relative to min wage)")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(0.5)4)
    saving(${figs}fcontrmw`year1'_`year3', replace);

 *graph export ${figs}fcontrmw`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * percentage terms;

  graph twoway scatter imss_emp_contrib_pct_`year1'-imss_emp_contrib_pct_`year3'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(5)30)
    saving(${figs}fcontrpct`year1'_`year3', replace);

 *graph export ${figs}fcontrpct`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter imss_emp_contrib_pct_`year1'-imss_emp_contrib_pct_`year3'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(5)30)
    saving(${figs}fcontrpctlow`year1'_`year3', replace);

 *graph export ${figs}fcontrpctlow`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * percentage terms relative to min wage;

  graph twoway scatter imss_emp_contrib_pct_`year1'-imss_emp_contrib_pct_`year3'
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(5)30)
    saving(${figs}fcontrpctmw`year1'_`year3', replace);

 *graph export ${figs}fcontrpctmw`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * absolute changes;

 graph twoway scatter tot_emp_contrib_`year1'_`year1a'-tot_emp_contrib_`year3'_`year3a'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("change in employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(-20(10)20)
    saving(${figs}fcontrchg`year1'_`year3', replace);

 *graph export ${figs}fcontrchg`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter tot_emp_contrib_`year1'_`year1a'-tot_emp_contrib_`year3'_`year3a'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("change in employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(-10(2)8)
    saving(${figs}fcontrlowchg`year1'_`year3', replace);

 *graph export ${figs}fcontrlowchg`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * percentage changes;

  graph twoway scatter imss_emp_contrib_pct_`year1'_`year1a'-imss_emp_contrib_pct_`year3'_`year3a'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("change in employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(-10(5)10)
    saving(${figs}fcontrpctchg`year1'_`year3', replace);

 *graph export ${figs}fcontrpctchg`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter imss_emp_contrib_pct_`year1'_`year1a'-imss_emp_contrib_pct_`year3'_`year3a'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("change in employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(-10(5)10)
    saving(${figs}fcontrpctlowchg`year1'_`year3', replace);

 *graph export ${figs}fcontrpctlowchg`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;


};

graph combine ${figs}fcontr1988_1991.gph ${figs}fcontr1992_1995.gph ${figs}fcontr1996_1999.gph
    ${figs}fcontr2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontr, replace);

graph export ${figs}fcontr.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrmw1988_1991.gph ${figs}fcontrmw1992_1995.gph ${figs}fcontrmw1996_1999.gph
    ${figs}fcontrmw2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrmw, replace);

graph export ${figs}fcontrmw.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrlow1988_1991.gph ${figs}fcontrlow1992_1995.gph ${figs}fcontrlow1996_1999.gph
    ${figs}fcontrlow2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrlow, replace);

graph export ${figs}fcontrlow.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrpct1988_1991.gph ${figs}fcontrpct1992_1995.gph ${figs}fcontrpct1996_1999.gph
    ${figs}fcontrpct2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrpct, replace);

graph export ${figs}fcontrpct.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrpctmw1988_1991.gph ${figs}fcontrpctmw1992_1995.gph ${figs}fcontrpctmw1996_1999.gph
    ${figs}fcontrpctmw2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrpctmw, replace);

graph export ${figs}fcontrpctmw.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrpctlow1988_1991.gph ${figs}fcontrpctlow1992_1995.gph
    ${figs}fcontrpctlow1996_1999.gph
    ${figs}fcontrpctlow2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrpctlow, replace);

graph export ${figs}fcontrpctlow.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrchg1988_1991.gph ${figs}fcontrchg1992_1995.gph ${figs}fcontrchg1996_1999.gph
    ${figs}fcontrchg2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrchg, replace);

graph export ${figs}fcontrchg.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fcontrpctchg1988_1991.gph ${figs}fcontrpctchg1992_1995.gph ${figs}fcontrpctchg1996_1999.gph
    ${figs}fcontrpctchg2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fcontrchgpct, replace);

graph export ${figs}fcontrchgpct.ps, logo(off) mag(100) orientation(landscape) replace;


*absolute terms;

graph twoway scatter tot_emp_contrib_1990 tot_emp_contrib_1993  tot_emp_contrib_1997
   tot_emp_contrib_2000  tot_emp_contrib_2003
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(50)200)
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fcontr_summary, replace);

graph export ${figs}fcontr_summary.ps, logo(off) mag(100) orientation(landscape) replace;

*absolute terms, low wage values;

graph twoway scatter tot_emp_contrib_1990 tot_emp_contrib_1993  tot_emp_contrib_1997
   tot_emp_contrib_2000  tot_emp_contrib_2003
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(10)50)
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fcontr_summary_low, replace);

graph export ${figs}fcontr_summary_low.ps, logo(off) mag(100) orientation(landscape) replace;

*absolute terms, relative to min wage;

graph twoway scatter tot_emp_contrib_sal_mw_1990 tot_emp_contrib_sal_mw_1993  tot_emp_contrib_sal_mw_1997
   tot_emp_contrib_sal_mw_2000 tot_emp_contrib_sal_mw_2003
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution (relative to min wage)")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(.5)4)
    saving(${figs}fcontrmw_summary, replace);

graph export ${figs}fcontrmw_summary.ps, logo(off) mag(100) orientation(landscape) replace;

* percentage terms;

graph twoway scatter imss_emp_contrib_pct_1990 imss_emp_contrib_pct_1993 imss_emp_contrib_pct_1997
   imss_emp_contrib_pct_2000  imss_emp_contrib_pct_2003
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(5)30)
    xsize(10)
    ysize(7.5)
    saving(${figs}fcontrpct_summary, replace);

graph export ${figs}fcontrpct_summary.ps, logo(off) mag(100) orientation(landscape) replace;

* percentage terms, low wage values;

graph twoway scatter imss_emp_contrib_pct_1990 imss_emp_contrib_pct_1993 imss_emp_contrib_pct_1997
   imss_emp_contrib_pct_2000  imss_emp_contrib_pct_2003
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(5)30)
    xsize(10)
    ysize(7.5)
    saving(${figs}fcontrpct_summary_low, replace);

graph export ${figs}fcontrpct_summary_low.ps, logo(off) mag(100) orientation(landscape) replace;

* percentage terms, relative to min wage;

graph twoway scatter imss_emp_contrib_pct_1990 imss_emp_contrib_pct_1993  imss_emp_contrib_pct_1997
   imss_emp_contrib_pct_2000  imss_emp_contrib_pct_2003
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("employer contribution, as % of wage")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(5)30)
    saving(${figs}fcontrpctmw_summary, replace);

graph export ${figs}fcontrpctmw_summary.ps, logo(off) mag(100) orientation(landscape) replace;


***********************************************************************************;
********* Fig A2 (fworkercontr_summary): worker contribution schedules, by year ********;
***********************************************************************************;

*** get worker contributions file ready;

clear all;
use ${work}imss_worker_contrib;
sort year qtr;
save `d1', replace;

*** get minimum wages file ready;

* Here I am just keeping minimum wages for zone A (Mexico City). May
  want to come back to this;

clear all;
use ${work}minwages_wtopcodes;
keep if zona=="A";
keep year qtr salmin salmindf cpi;
sort year qtr;
save `d2', replace;

*** make fake wage data;

local fyr=1985;
local lyr=2005;
local nyrs = `lyr'-`fyr'+1;
local ninds = 1000; * number of fake individuals;
local nobs = `nyrs'*`ninds';
local highestrsalmin = 100.9658;
local lowestrsalmin = 37.87585;

clear all;
set obs `nobs';
gen year = ceil(_n/`ninds')+`fyr'-1;
gen qtr = 3;
bysort year: gen rsal = (_n/`ninds')*(floor(`highestrsalmin')*30);

keep year qtr rsal;

*** merge in worker contribution percentages;

sort year qtr;
merge year qtr using `d1', uniqusing;
tab _merge;
keep if _merge==3;
drop _merge;

*** merge in minimum wages, cpi;

sort year qtr;
merge year qtr using `d2', uniqusing;
tab _merge;
keep if _merge==3;
drop _merge;

gen sal = rsal*(cpi/100);
gen rsalmin = salmin/(cpi/100);

* Note: here just assume we are in zone A (Mexico City);

* Note: the topcode for cpda (INFONAVIT) depends on local minimum
  wage, not Mexico City minimum wage;

* this variable is the value of the worker contribution, from contributions_imss.do;
gen imss_worker_contrib = (

                    (sal<=limit_sem*salmindf)*(sem/100)*sal +
                    (sal>limit_sem*salmindf)*(sem/100)*limit_sem*salmindf +
                    (sal<=limit_sem*salmindf)*(sem_d/100)*sal +
                    (sal>limit_sem*salmindf)*(sem_d/100)*limit_sem*salmindf +

                    (sal<=limit_siv*salmindf)*(sivcm/100)*sal +
                    (sal>limit_siv*salmindf)*(sivcm/100)*limit_siv*salmindf +
                    (sal<=limit_siv*salmindf)*(siv/100)*sal +
                    (sal>limit_siv*salmindf)*(siv/100)*limit_siv*salmindf +

                    (sal<=limit_srcv_r*salmindf)*(srcv_r/100)*sal +
                    (sal>limit_srcv_r*salmindf)*(srcv_r/100)*limit_srcv_r*salmindf +

                    (sal<=limit_srcv_c*salmindf)*(srcv_c/100)*sal +
                    (sal>limit_srcv_c*salmindf)*(srcv_c/100)*limit_srcv_c*salmindf +

                    (sal<=limit_sgps*salmindf)*(sgps/100)*sal +
                    (sal>limit_sgps*salmindf)*(sgps/100)*limit_sgps*salmindf +

                    (sal<=limit_cpda*salmin)*(cpda/100)*sal +
                    (sal>limit_cpda*salmin)*(cpda/100)*limit_cpda*salmin +

                    (sal<=limit_siv*salmindf)*(aport_esp/100)*sal +
                    (sal>limit_siv*salmindf)*(aport_esp/100)*limit_siv*salmindf +

                    (sal<=limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(sal-3*salmindf) +
                    (sal>limit_sem*salmindf)*(sal>=3*salmindf)*(extra/100)*(limit_sem*salmindf-3*salmindf) +

                    (sem_fija/100)*salmindf

                    );

gen imss_worker_contrib_pct = (imss_worker_contrib / sal)*100;

gen sal_mw = sal / salmindf;

save `d3', replace;

**** make graphs;

local i = 1985;
while `i'<=2005 {;
                 gen imss_worker_contrib_pct_`i' = imss_worker_contrib_pct if year==`i';
                 label variable imss_worker_contrib_pct_`i' "`i'";
                 local i=`i'+1;
               };

gen tot_worker_contrib = (imss_worker_contrib_pct/100) * rsal;
gen tot_worker_contrib_sal_mw = (imss_worker_contrib_pct/100) * sal_mw;

local i = 1985;
while `i'<=2005 {;
                 gen tot_worker_contrib_`i' = tot_worker_contrib if year==`i';
                 label variable tot_worker_contrib_`i' "`i'";

                 local i=`i'+1;
               };
local i = 1985;
while `i'<=2005 {;
                 gen tot_worker_contrib_sal_mw_`i' = tot_worker_contrib_sal_mw if year==`i';
                 label variable tot_worker_contrib_sal_mw_`i' "`i'";

                 local i=`i'+1;
               };

foreach year1 in 1988 1992 1996 2000
{;
 forvalues i = 2/4
 {;
  local year`i' = `year1'+`i';
 };

 * absolute terms;

 graph twoway scatter tot_worker_contrib_`year1'-tot_worker_contrib_`year3'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(10)60)
    saving(${figs}fworkercontr`year1'_`year3', replace);

 *graph export ${figs}fworkercontr`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter tot_worker_contrib_`year1'-tot_worker_contrib_`year3'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrlow`year1'_`year3', replace);

 *graph export ${figs}fworkercontrlow`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * absolute terms relative to min wage;

 graph twoway scatter tot_worker_contrib_sal_mw_`year1'-tot_worker_contrib_sal_mw_`year3'
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (relative to min wage)")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(0.25)1)
    saving(${figs}fworkercontrmw`year1'_`year3', replace);

 *graph export ${figs}fworkercontrmw`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * percentage terms;

  graph twoway scatter imss_worker_contrib_pct_`year1'-imss_worker_contrib_pct_`year3'
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrpct`year1'_`year3', replace);

 *graph export ${figs}fworkercontrpct`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 graph twoway scatter imss_worker_contrib_pct_`year1'-imss_worker_contrib_pct_`year3'
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrpctlow`year1'_`year3', replace);

 *graph export ${figs}fworkercontrpctlow`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

 * percentage terms relative to min wage;

  graph twoway scatter imss_worker_contrib_pct_`year1'-imss_worker_contrib_pct_`year3'
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution, as % of wage")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrpctmw`year1'_`year3', replace);

 *graph export ${figs}fworkercontrpctmw`year1'_`year3'.ps, logo(off) mag(100) orientation(landscape) replace;

};

graph combine ${figs}fworkercontr1988_1991.gph ${figs}fworkercontr1992_1995.gph ${figs}fworkercontr1996_1999.gph
    ${figs}fworkercontr2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7)
    saving(${figs}fworkercontr, replace);

graph export ${figs}fworkercontr.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fworkercontrlow1988_1991.gph ${figs}fworkercontrlow1992_1995.gph ${figs}fworkercontrlow1996_1999.gph
    ${figs}fworkercontrlow2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7)
    saving(${figs}fworkercontrlow, replace);

graph export ${figs}fworkercontrlow.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fworkercontrmw1988_1991.gph ${figs}fworkercontrmw1992_1995.gph ${figs}fworkercontrmw1996_1999.gph
    ${figs}fworkercontrmw2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fworkercontrmw, replace);

graph export ${figs}fworkercontrmw.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fworkercontrpct1988_1991.gph ${figs}fworkercontrpct1992_1995.gph ${figs}fworkercontrpct1996_1999.gph
    ${figs}fworkercontrpct2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7)
    saving(${figs}fworkercontrpct, replace);

graph export ${figs}fworkercontrpct.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fworkercontrpctlow1988_1991.gph ${figs}fworkercontrpctlow1992_1995.gph
    ${figs}fworkercontrpctlow1996_1999.gph
    ${figs}fworkercontrpctlow2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7)
    saving(${figs}fworkercontrpctlow, replace);

graph export ${figs}fworkercontrpctlow.ps, logo(off) mag(100) orientation(landscape) replace;

graph combine ${figs}fworkercontrpctmw1988_1991.gph ${figs}fworkercontrpctmw1992_1995.gph ${figs}fworkercontrpctmw1996_1999.gph
    ${figs}fworkercontrpctmw2000_2003.gph, scheme(s1mono) iscale(*.6) xsize(9) ysize(7) xcommon ycommon
    saving(${figs}fworkercontrpctmw, replace);

graph export ${figs}fworkercontrpctmw.ps, logo(off) mag(100) orientation(landscape) replace;

*absolute terms;

graph twoway scatter tot_worker_contrib_1990 tot_worker_contrib_1993  tot_worker_contrib_1997
   tot_worker_contrib_2000  tot_worker_contrib_2003
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(10)50)
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fworkercontr_summary, replace);

graph export ${figs}fworkercontr_summary.ps, logo(off) mag(100) orientation(landscape) replace;

*absolute terms, low wage levels;

graph twoway scatter tot_worker_contrib_1990 tot_worker_contrib_1993  tot_worker_contrib_1997
   tot_worker_contrib_2000  tot_worker_contrib_2003
    rsal if rsal>=rsalmin & rsal<=200,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (constant 2002 pesos/day)")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(5)10)
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fworkercontr_summary_low, replace);

graph export ${figs}fworkercontr_summary_low.ps, logo(off) mag(100) orientation(landscape) replace;

*absolute terms, relative to min wage;

graph twoway scatter tot_worker_contrib_sal_mw_1990 tot_worker_contrib_sal_mw_1993  tot_worker_contrib_sal_mw_1997
   tot_worker_contrib_sal_mw_2000 tot_worker_contrib_sal_mw_2003
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution (relative to min wage)")
    xtitle("wage relative to min wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(4) ring(0) size(small))
    ylab(0(.25)1)
    saving(${figs}fworkercontrmw_summary, replace);

graph export ${figs}fworkercontrmw_summary.ps, logo(off) mag(100) orientation(landscape) replace;

* percentage terms;

graph twoway scatter imss_worker_contrib_pct_1990 imss_worker_contrib_pct_1993  imss_worker_contrib_pct_1997
   imss_worker_contrib_pct_2000  imss_worker_contrib_pct_2003
    rsal if rsal>=rsalmin & rsal<=1500,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution, as % of wage")
    xtitle("real wage (constant 2002 pesos/day)") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrpct_summary, replace);

graph export ${figs}fworkercontrpct_summary.ps, logo(off) mag(100) orientation(landscape) replace;


* percentage terms, relative to min wage;

graph twoway scatter imss_worker_contrib_pct_1990 imss_worker_contrib_pct_1993  imss_worker_contrib_pct_1997
   imss_worker_contrib_pct_2000  imss_worker_contrib_pct_2003
    sal_mw if sal_mw>=1 & sal_mw<=25,
    msymbol(i i i i i i)
    connect(l l l l l l) clwidth(medthick medthick medthick medthick medthick medthick)
    clcolor(blue green red black orange) sort
    ytitle("worker contribution, as % of wage")
    xtitle("wage relative to minimum wage") scheme(s1mono) xscale(titlegap(2))
    yscale(titlegap(2)) legend(pos(2) ring(0) size(small))
    ylab(0(2)10)
    saving(${figs}fworkercontrpctmw_summary, replace);

graph export ${figs}fworkercontrpctmw_summary.ps, logo(off) mag(100) orientation(landscape) replace;



***************************************************************************************;
******* Fig A4 (fempl): employment over time (IMSS, ENEU w/ imss ENEU w/out imss) **********;
***************************************************************************************;

******** separately by sex **************;
clear;
use ${work}cellpqn_age_cat2_19882003;

keep if qtr==2;

sort year qtr male;
collapse (sum) empl_eneu_ft_imss empl_eneu_imss empl_eneu_noimss empl_eneu_perm_imss
    empl_imss, by(year qtr male);

replace empl_eneu_perm_imss = . if empl_eneu_perm_imss==0;

gen lempl_eneu_ft_imss = log(empl_eneu_ft_imss);
gen lempl_eneu_imss = log(empl_eneu_imss);
gen lempl_eneu_noimss = log(empl_eneu_noimss);
gen lempl_imss = log(empl_imss);

replace empl_eneu_ft_imss = empl_eneu_ft_imss/1000000;
replace empl_eneu_imss = empl_eneu_imss/1000000;
replace empl_eneu_noimss = empl_eneu_noimss/1000000;
replace empl_imss = empl_imss/1000000;
replace empl_eneu_perm_imss = empl_eneu_perm_imss/1000000;

graph twoway scatter empl_imss empl_eneu_ft_imss empl_eneu_imss empl_eneu_perm_imss empl_eneu_noimss
    year if male==1,
    msymbol(i i i i i) connect(l l l l l) clwidth(medthick medthick medthick medthick medthick)
    clcolor(blue green red purple orange) sort
    ytitle("employment (millions)")
    xtitle("Year")
    scheme(s1mono)
    xscale(titlegap(2))
    yscale(titlegap(2))
    xline(1995.4, lwidth(medthick) lpattern(dash) lcolor(gs4))
    xline(1997, lwidth(medthick) lpattern(solid) lcolor(gs4))
    xlab(1988(2)2002)
    ylab(0(.5)2.5)
    legend(label(1 "IMSS admin. records") label(2 "ENEU full-time w/ IMSS") label(3 "ENEU w/ IMSS")
           label(4 "ENEU permanent w/ IMSS") label(5 "ENEU w/out IMSS")
             pos(5) ring(0) size(small))
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fempl_male1, replace);

graph export ${figs}fempl_male1.ps, logo(off) mag(100) orientation(landscape) replace;


graph twoway scatter empl_imss empl_eneu_ft_imss empl_eneu_imss empl_eneu_perm_imss empl_eneu_noimss
    year if male==0,
    msymbol(i i i i i) connect(l l l l l) clwidth(medthick medthick medthick medthick medthick)
    clcolor(blue green red purple orange) sort
    ytitle("employment (millions)")
    xtitle("Year")
    scheme(s1mono)
    xscale(titlegap(2))
    yscale(titlegap(2))
    xline(1995.4, lwidth(medthick) lpattern(dash) lcolor(gs4))
    xline(1997, lwidth(medthick) lpattern(solid) lcolor(gs4))
    xlab(1988(2)2002)
    ylab(0(.5)1.5)
    legend(label(1 "IMSS admin. records") label(2 "ENEU full-time w/ IMSS") label(3 "ENEU w/ IMSS")
           label(4 "ENEU permanent w/ IMSS") label(5 "ENEU w/out IMSS")
             pos(5) ring(0) size(small))
    xsize(10)
    ysize(7.5)
    scale(.8)
    saving(${figs}fempl_male0, replace);

graph export ${figs}fempl_male0.ps, logo(off) mag(100) orientation(landscape) replace;


****************************************************************************************************;
********* Fig A5 (fhistsexpub_yr): histograms by sex, public sector *********;
****************************************************************************************************;

local poolqtrs = "y";
local yr=1990;
local qtr=2;
local male=1;

 
   local month=`qtr'*3;
   di "year = `yr', quarter = `qtr', month = `month'";
   clear;
   use ${work}imss_forhh_wpubsec_`yr'`month';


    di "year = `yr', quarter = `qtr', month = `month', male = `male'";

    preserve;

    ***** get IMSS data ready ****;

    keep if male == `male';

    keep if sector == 4; * public sector;

    * frac 9403 is seguridad social in IMSS industry classification;

    keep if frac=="9403";

    save `d1', replace;

    ***** get ENEU data ready ****;

    clear;
    use ${work}eneu_forimss_wpubsec_19882003;

    keep if year==`yr' & male == `male' & work_ft_imss == 1;

    keep if sector==4; * public sector;

    * subgrupo 7321 is seguridad social (including ISSSTE, IMSS etc.)
    * in both CAE 1989 and CAE 1994;

       keep if subgrupo==7321;

    * make ENEU variable with same name as IMSS variable;
    gen rsal7net = rsal7;

    tab age_cat2, missing;

    *find and save min wages;
    tab rsalmin if qtr==`qtr', matrow(salmin);
    local salmin1=salmin[1,1];
    local salmin2=salmin[2,1];
    local salmin3=salmin[3,1];

    **** stack IMSS and ENEU data ****;

    append using `d1';

    **** make histograms ***;

    twoway (histogram rsal7 if imss==1, width(5) start(0) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7net if eneu==1 & work_ft_imss==1 [fw=factor],
        width(5) start(0)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        title("A. IMSS pre-tax vs. ENEU, full distribution")
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(0(50)550) ytitle("fraction of sample") ylab(0(0.05)0.10)
        legend(label(1 "IMSS admin. records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector', replace);

    graph export ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'.ps,
          logo(off) mag(100) orientation(landscape) replace;

    twoway (histogram rsal7net if imss==1, width(5) start(0) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7net if eneu==1 & work_ft_imss==1 [fw=factor],
        width(5) start(0)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        title("B. IMSS post-tax vs. ENEU, full distribution")
        xtitle("real daily salary (constant 2002 pesos)")
        xlab(0(50)550) ytitle("fraction of sample") ylab(0(0.05)0.10)
        legend(label(1 "IMSS admin. records (post-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post, replace);

    graph export ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post.ps,
          logo(off) mag(100) orientation(landscape) replace;

    twoway (histogram rsal7 if imss==1, width(2) start(30) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7net if eneu==1 & work_ft_imss==1 [fw=factor],
        width(2) start(30)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction)
        if inrange(rsal7net,30,200),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
        xtitle("real daily salary (constant 2002 pesos)")
       xlab(40(20)200) ytitle("fraction of sample") ylab(0(0.05)0.10)
       xscale(range(30 200))
        legend(label(1 "IMSS admin. records (pre-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_low, replace);


    graph export ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_low.ps, logo(off)
          mag(100) orientation(landscape) replace;


    twoway (histogram rsal7net if imss==1, width(2) start(30) fcolor(gs8)
        lcolor(gs8) lwidth(vvthin) fraction)
        (histogram rsal7net if eneu==1 & work_ft_imss==1 [fw=factor],
        width(2) start(30)
        fcolor(none) lcolor(gs0) lwidth(thin) fraction)
        if inrange(rsal7net,30,200),
        xline(`salmin1' `salmin2' `salmin3', lwidth(vvthin))
             title("C. IMSS post-tax vs. ENEU, low wages")
        xtitle("real daily salary (constant 2002 pesos)")
       xlab(40(20)200) ytitle("fraction of sample") ylab(0(0.05)0.10)
       xscale(range(30 200))
        legend(label(1 "IMSS admin. records (post-tax)")
               label(2 "ENEU household survey")
               pos(1) ring(0) rows(2) size(vsmall))
        scheme(s1mono)
        xsize(10) ysize(7.5)
        saving(${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post_low, replace);

    graph export ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post_low.ps, logo(off)
          mag(100) orientation(landscape) replace;


graph combine ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'.gph
     ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post.gph
     ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_post_low.gph, scheme(s1mono)
     iscale(*.7) xsize(10) ysize(7.5) ycommon
    saving(${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_comb.gph, replace);

        graph export ${figs}fhistsexpub_yr`yr'_`qtr'_`male'_`poolqtrs'_`sector'_comb.ps, logo(off)
          mag(100) orientation(landscape) replace;

    restore;



***************************************************************************************;
***** Fig A6 (fexplainexcmass_p15): figure to explain excess mass calculation *********;
***************************************************************************************;

local yr = 1990;
local qtr = 2;
local male=1;

local bwidth_imss = 3;
local bwidth_eneu = 6;
local npoints = 200; * number of points used to estimate density;


local month=`qtr'*3;
di "year = `yr', quarter = `qtr', month = `month'";
clear;

use ${work}imss_forhh_`yr'`month';

keep if male == `male';

tempfile d1;
save `d1', replace;

********** get ENEU data ready **********;

clear;
use ${work}eneu_forimss_19882003;

keep if year==`yr' & male == `male';

keep if qtr==2;

* rename ENEU variable so it is the same as IMSS variable;
gen rsal7net = rsal7;

******* stack IMSS and ENEU data  ********;

append using `d1';

********** calculate densities **********;

kdensity rsal7net if (imss==1 | (eneu==1 & work_ft_imss==1)), nograph
    bwidth(2) n(`npoints') gen(x fx);
kdensity rsal7net if imss==1, nograph bwidth(`bwidth_imss') at(x) gen(fx_imss);
kdensity rsal7net if eneu==1 & work_ft_imss==1 [aw = factor], nograph bwidth(`bwidth_eneu')
    at(x) gen(fx_eneu_ft_imss);

su rsal7net if eneu==1 [aw=factor], detail;
local eneu_p25 = r(p25);
local eneu_p50 = r(p50);
di "p25 = `eneu_p25', p50 = `eneu_p50'";

egen eneu_p15 = pctile(rsal7net) if eneu==1, p(15);
local eneu_p15 = eneu_p15;

line fx_imss fx_eneu x,
    clwidth(medthin medthin)
    clpattern(solid dash)
    clcolor(red blue)
    xtitle("real daily wage, `yr'")
         xlab(0(100)500)
         ytitle("density")
	 xline(`eneu_p15', lwidth(medthin) lcolor(gs12))
         ylab(0(0.01)0.03)
         legend(label(1 "IMSS admin. records (post-tax)")
                label(2 "ENEU household survey")
                pos(2) ring(0) cols(1) size(small))
         scheme(s1mono)
         xscale(titlegap(2))
         yscale(titlegap(2))
    xsize(10) ysize(7.5)
    scale(.8)
    saving(${figs}fexplainexcmass_p15, replace);

graph export ${figs}fexplainexcmass_p15.ps, logo(off) mag(100)
      orientation(landscape) lmargin(.1) replace;


*******************************************************************************;
********* Fig A7 (fdists): IMSS vs. ENEU smoothed densities *******;
*******************************************************************************;

* parameters;
local fyr = 1988;
local lyr = 2003;
local fqtr = 2;
local lqtr = 2;
local fage_cat2 = 1;
local lage_cat2 = 5;
local bwidth_imss = 3;
local bwidth_eneu = 6;
local npoints = 200; * number of points used to estimate density;

local age_cat2label1 = "ages 16-25";
local age_cat2label2 = "ages 26-35";
local age_cat2label3 = "ages 36-45";
local age_cat2label4 = "ages 46-55";
local age_cat2label5 = "ages 56-65";

foreach poolqtrs in "n"
{;

 di "poolqtrs = `poolqtrs'";

 foreach yr in 1990 1997 2003
 {;
  di "yr = `yr'";

  foreach qtr in 2
  {;
   di "qtr = `qtr'";

   local month=`qtr'*3;
   di "year = `yr', quarter = `qtr', month = `month'";
   clear;
   use ${work}imss_forhh_`yr'`month';

   forval age_cat2 = `fage_cat2'/`lage_cat2'
   {;
    di "age_cat = `age_cat2'";

    forval male = 0/1
    {;
     di "male = `male'";

     di "year = `yr', quarter = `qtr', month = `month', age_cat2 = `age_cat2', male = `male'";

     preserve;

     ********** get IMSS data ready **********;

     keep if age_cat2 == `age_cat2' & male == `male';

     *find and save min wages;
     tab rsalmin, matrow(salmin);
     local salmin1=salmin[1,1];
     local salmin2=salmin[2,1];
     local salmin3=salmin[3,1];

     save `d1', replace;

     ********** get ENEU data ready **********;

     clear;
     use ${work}eneu_forimss_19882003;

     if "`poolqtrs'"=="n"
     {;
      keep if year==`yr' & qtr == `qtr' & age_cat2 == `age_cat2' & male == `male';
    };

     * specification pooling ENEU data across quarters within year;
     if "`poolqtrs'"=="y"
     {;
      keep if year==`yr' & age_cat2 == `age_cat2' & male == `male';
    };

     * rename ENEU wage variable so it has same name as IMSS post-tax variable;
     gen rsal7net = rsal7 if eneu==1;

     ******* stack IMSS and ENEU data  ********;

     append using `d1';

     ********** calculate densities **********;

     kdensity rsal7net if (imss==1 | (eneu==1 & work_ft_imss==1)), nograph
         bwidth(2) n(`npoints') gen(x fx);
     kdensity rsal7net if imss==1, nograph bwidth(`bwidth_imss') at(x) gen(fx_imss);
     kdensity rsal7net if eneu==1 & work_ft_imss==1 [aw = factor], nograph bwidth(`bwidth_eneu')
         at(x) gen(fx_eneu_ft_imss);

     line fx_imss fx_eneu x,
         clwidth(medthin medthin)
         clpattern(solid dash)
         clcolor(red blue)
         xtitle("real daily wage, `age_cat2label`age_cat2''")
         xlab(0(100)500)
         ytitle("density, `yr'")
	 xline(`salmin1' `salmin2' `salmin3', lwidth(vthin) lcolor(gs12))
         ylab(0(0.01)0.03)
         legend(label(1 "IMSS admin. records (post-tax)")
                label(2 "ENEU household survey")
                pos(2) ring(0) cols(1) size(tiny))
         scheme(s1mono)
         xscale(titlegap(2))
         yscale(titlegap(2))
         saving(${figs}fdists_ft_`yr'_`qtr'_`age_cat2'_`male'_bw`bwidth_eneu'_`poolqtrs', replace);

     line fx_imss fx_eneu x if x<=200,
         xlab(0(50)200)
         clwidth(medthin medthin)
         clpattern(solid dash)
         clcolor(red blue)
         xtitle("real daily wage, `age_cat2label`age_cat2''")
         ytitle("density, `yr'")
	 xline(`salmin1' `salmin2' `salmin3', lwidth(vthin) lcolor(gs12))
         ylab(0(0.01)0.03)
         legend(label(1 "IMSS admin. records (post-tax)")
                label(2 "ENEU household survey")
                pos(2) ring(0) cols(1) size(tiny))
         xscale(titlegap(2))
         yscale(titlegap(2))
         scheme(s1mono)
         saving(${figs}fdists_ft_`yr'_`qtr'_`age_cat2'_`male'_bw`bwidth_eneu'_`poolqtrs'_low,
                replace);

     * save estimates for analysis below;
     keep year qtr age_cat2 male x fx_imss fx_eneu_ft_imss rsal7net work_imss imss eneu;


     restore;

    };
   };
  };
 };


 ******* make combined graphs, to see changes in stacking over time *************;

 local qtr=2;

 foreach male in 0 1
 {;
di "male = `male'";

 grc1leg ${figs}fdists_ft_1990_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1997_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_2003_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1990_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1997_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_2003_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1990_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1997_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_2003_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1990_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1997_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_2003_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1990_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_1997_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'.gph
      ${figs}fdists_ft_2003_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'.gph,
      scheme(s1mono) iscale(*.8) xsize(10) ysize(7) xcommon ycommon rows(3) pos(5) ring(2) colfirst
      saving(${figs}fdists_ft_comb_`qtr'_`male'_bw`bwidth_eneu'_`poolqtrs', replace);


  graph export ${figs}fdists_ft_comb_`qtr'_`male'_bw`bwidth_eneu'_`poolqtrs'.ps, logo(off)
     mag(150) lmargin(.05) orientation(landscape) replace;

  grc1leg ${figs}fdists_ft_1990_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1997_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_2003_`qtr'_1_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1990_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1997_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_2003_`qtr'_2_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1990_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1997_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_2003_`qtr'_3_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1990_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1997_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_2003_`qtr'_4_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1990_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_1997_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph
      ${figs}fdists_ft_2003_`qtr'_5_`male'_bw`bwidth_eneu'_`poolqtrs'_low.gph,
      scheme(s1mono) iscale(*.8) xsize(10) ysize(7) xcommon ycommon rows(3) pos(5) ring(2) colfirst
      saving(${figs}fdists_ft_comb_`qtr'_`male'_bw`bwidth_eneu'_`poolqtrs'_low, replace);


  graph export ${figs}fdists_ft_comb_`qtr'_`male'_bw`bwidth_eneu'_`poolqtrs'_low.ps, logo(off)
      mag(150) lmargin(.05) orientation(landscape) replace;

 };

};



log close;
